package cn.zqh.auth.domain;

import cn.zqh.auth.mapper.UserMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 登录用户信息
 * Created by macro on 2020/6/19.
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SecurityUserPlus implements UserDetails {

    @Resource
    private UserMapper userMapper;

    /**
     * ID
     */
    private String id;
    /**
     * 用户名
     */
    private String qqEmail;
    /**
     * 用户密码
     */
    private String password;
    /**
     * 用户状态
     */
    private Boolean enabled;
    /**
     * 权限数据
     */
    private Collection<? extends GrantedAuthority> authorities;

    public SecurityUserPlus() {

    }

//    public SecurityUserPlus(UserPlus userPlus,List<String> roleList) {
////        List<String> roleCodeByEmail = userMapper.getRoleCodeByEmail(userPlus.getQqEmail());
//        this.setId(userPlus.getId());
//        this.setQqEmail(userPlus.getQqEmail());
//        this.setPassword(userPlus.getPassword());
//        this.setEnabled(userPlus.getEnabled());
//        if (roleList != null) {
//            authorities = new ArrayList<>();
//            roleList.forEach(item -> authorities.add(new SimpleGrantedAuthority(item)));
//        }
//    }



    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.qqEmail;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }


}
